package com.bw.module_home.repair.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_home.repair.model.AddRepairEntity
import com.bw.module_home.repair.model.RepairEntity
import com.bw.module_home.repair.model.RepairEntityItem
import com.bw.module_home.repair.model.RepairRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject

@HiltViewModel
class RepairViewModel @Inject constructor(
    private val repairRepository: RepairRepository
) : BaseViewModel() {

    // 将 MutableList 改为 MutableLiveData
    val getRepairListSuccess = MutableLiveData<List<RepairEntity>>()
    val addRepairSuccess = MutableLiveData<List<RepairEntityItem>>()
    val deleteRepairSuccess = MutableLiveData<List<Boolean>>()
    val getRepairOrderByNumberSuccess = MutableLiveData<List<RepairEntityItem>>()
    val getRepairOrderByNameSuccess = MutableLiveData<List<RepairEntity>>()

    fun getRepairList() {
        repairRepository.getRepairList()
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        // 更新 LiveData 的值
                        val currentList = getRepairListSuccess.value?.toMutableList() ?: mutableListOf()
                        currentList.add(it)
                        getRepairListSuccess.value = currentList
                    }
                } else {
                    Log.e("RepairViewModel", "获取报修列表失败  ${result.message}")
                }
            }
            .catch { e ->
                Log.e("RepairViewModel", "获取报修列表异常: ${e.message}")
            }
            .launchIn(viewModelScope)
    }

    fun addRepair(addRepairEntity: AddRepairEntity) {
        repairRepository.addRepair(addRepairEntity)
            .onEach { result ->
                Log.d("add_repair", "addRepair:${result.data}")
                if (result.code == 200) {
                    result.data?.let {
                        // 更新 LiveData 的值
                        val currentList = addRepairSuccess.value?.toMutableList() ?: mutableListOf()
                        currentList.add(it)
                        addRepairSuccess.value = currentList
                    }
                } else {
                    Log.e("RepairViewModel", "添加报修失败 ${result.message}")
                }
            }
            .catch { e ->
                Log.e("RepairViewModel", "添加报修异常: ${e.message}")
            }
            .launchIn(viewModelScope)
    }

    fun deleteRepair(id: Int) {
        repairRepository.deleteRepair(id)
            .onEach { result ->
                Log.d("delete_repair", "deleteRepair:${result.data}")
                if (result.code == 200) {
                    result.data?.let {
                        // 更新 LiveData 的值
                        val currentList = deleteRepairSuccess.value?.toMutableList() ?: mutableListOf()
                        currentList.add(it)
                        deleteRepairSuccess.value = currentList
                    }
                } else {
                    Log.e("RepairViewModel", "删除维修失败 ${result.message}")
                }
            }
            .catch { e ->
                Log.e("RepairViewModel", "删除维修异常: ${e.message}")
            }
            .launchIn(viewModelScope)
    }

    fun getRepairOrderByOrderNumber(orderNumber: String) {
        repairRepository.getRepairOrderByNumber(orderNumber)
            .onEach { result ->
                Log.d("get_repair_by_number", "getRepairOrderByOrderNumber: ${result.data}")
                if (result.code == 200) {
                    result.data?.let {
                        // 更新 LiveData 的值
                        val currentList = getRepairOrderByNumberSuccess.value?.toMutableList() ?: mutableListOf()
                        currentList.add(it)
                        getRepairOrderByNumberSuccess.value = currentList
                    }
                } else {
                    Log.e("RepairViewModel", "根据orderNumber获取失败 ${result.message}")
                }
            }
            .catch { e ->
                Log.e("RepairViewModel", "根据orderNumber获取异常: ${e.message}")
            }
            .launchIn(viewModelScope)
    }

    fun getRepairOrderByName(name: String) {
        repairRepository.getRepairOrderByName(name)
            .onEach { result ->
                Log.d("get_repair_by_name", "getRepairOrderByName:${result.data}")
                if (result.code == 200) {
                    result.data?.let {
                        // 更新 LiveData 的值
                        val currentList = getRepairOrderByNameSuccess.value?.toMutableList() ?: mutableListOf()
                        currentList.add(it)
                        getRepairOrderByNameSuccess.value = currentList
                    }
                } else {
                    Log.e("RepairViewModel", "根据name获取失败 ${result.message}")
                }
            }
            .catch { e ->
                Log.e("RepairViewModel", "根据name获取异常: ${e.message}")
            }
            .launchIn(viewModelScope)
    }
}